package cn.liccshop.core.service.shopPhoto.controller;


import cn.liccshop.core.entity.ShopPhoto;
import cn.liccshop.core.service.shopPhoto.service.IShopPhotoService;
import cn.liccshop.core.utils.ObjectToOtherUtils;
import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.entity.Result;
import cn.liccshop.sys.exception.MySystemException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.liccshop.sys.base.MyActionController;

import java.util.List;
import java.util.Map;

/**
 * @description  前端控制器
 *
 * @author LiCC
 * @since 2019-04-14
 */
@RestController
@RequestMapping("/shopPhoto")
public class ShopPhotoController extends MyActionController<ShopPhoto> {

    @Autowired
    private IShopPhotoService liShopPhotoService;

    @Override
    protected IService<ShopPhoto> getLiService(){
        return liShopPhotoService;
    }

    @Override
    public Result dataList(@RequestBody ShopPhoto entity) throws MySystemException {
        IService<ShopPhoto> service = this.theService();
        List<ShopPhoto> data = service.list(new QueryWrapper<>(entity).orderByAsc("photo_sort"));
        return new Result().setCode(Constants.OK).setData(data);
    }

    /**
     * 获取轮播图信息
     * disable: null 获取所有轮播图信息-规则：先按disabled降序，再按photo_sort升序，最后按create_date降序
     * 1 获取可以显示的轮播图信息-规则： 先按photo_sort升序，最后按create_date降序
     * 2 获取不可显示的轮播图信息-规则： 按create_date降序
     * @param page
     * @param size
     * @param map
     * @return
     * @throws MySystemException
     */
    @RequestMapping(value = "homeListByPage")
    public Result getHomeListByPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestBody Map<String, Object> map) throws MySystemException {
        Integer disabled = ObjectToOtherUtils.toInteger(map.get("disabled"));
        Wrapper<ShopPhoto> wrapper = new QueryWrapper<>(new ShopPhoto().setType(Constants.PHOTO_TYPE_HOME).setDisabled(disabled));
        if (disabled==null){
            /** 获取所有的轮播图信息 **/
//            ((QueryWrapper<ShopPhoto>) wrapper).orderByDesc("disabled").orderByAsc("photo_sort").orderByDesc("create_date");
            ((QueryWrapper<ShopPhoto>) wrapper).orderByDesc("create_date");
        } else if (disabled==1){
            /** 获取显示的轮播图信息 **/
            ((QueryWrapper<ShopPhoto>) wrapper).orderByAsc("photo_sort").orderByDesc("create_date");
        } else {
            /** 获取不显示的轮播图信息 **/
            ((QueryWrapper<ShopPhoto>) wrapper).orderByDesc("create_date");
        }
        IPage<ShopPhoto> iPage = new Page<>(page, size);
        this.theService().page(iPage, wrapper);
        return new Result().setCode(Constants.OK).setData(iPage);
    }

    /**
     * 获取前台走马灯（轮播图）信息
     * 按photo_sort降序排序
     * @return
     * @throws MySystemException
     */
    @RequestMapping(value = "previewCarousel")
    public Result previewCarousel() throws MySystemException {
        Wrapper<ShopPhoto> wrapper = new QueryWrapper<>(new ShopPhoto().setType(Constants.PHOTO_TYPE_HOME).setDisabled(1))
                .orderByAsc("photo_sort");
        List<ShopPhoto> data = liShopPhotoService.list(wrapper);
        return new Result().setCode(Constants.OK).setData(data).setMessage("获取信息成功");
    }
}

